﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace RobotEscape.Screens
{
    public class WinScreen : ScreenBase
    {
        GameScreen Parent;
        Rectangle Rectangle = new Rectangle();
        bool captcha = false;
        bool captchaFail = false;
        bool captchaPass = false;
        string captchaBuffer = "";

        public WinScreen(GameScreen gameScreen)
        {
            Parent = gameScreen;
        }

        public override void Init()
        {
            Canvas.SetLeft(Rectangle, 50);
            Canvas.SetTop(Rectangle, 50);
            Rectangle.Width = 300;
            Rectangle.Height = 500;
            Game.Instance.Canvas.Children.Add(Rectangle);
            if (Parent.levelNum == 4)
            {
                captcha = true;
                (Game.Instance.Canvas.Parent as UserControl).KeyUp += CaptchaKeyUp;
            }
            base.Init();
        }

        public override void Update(GameTime time)
        {
            ProcessKeys();
            Paint();
            base.Update(time);
        }

        private void ProcessKeys()
        {
            if ((KeyState.GetNewlyPressedKey(Key.Space) || KeyState.GetNewlyPressedKey(Key.Enter)) && (!captcha || captchaPass))
            {
                var level = Parent.levelNum + 1;
                this.Parent.Remove();
                Game.Instance.SwitchScreen(new GameScreen(level));
            }
        }

        private void Paint()
        {
            WriteableBitmap wb = new WriteableBitmap((int)this.Rectangle.Width, (int)this.Rectangle.Height);
            wb.Invalidate();
            wb.FillRectangle(0, 0, wb.PixelWidth, wb.PixelHeight, Util.BackgroundGrey);

            Util.DrawText(wb, "LEVEL " + Parent.levelNum + " COMPLETED!", 20, wb.PixelWidth / 2, 0, Colors.White, TextAlignment.Center);

            string gametime = String.Format("{0:N0}:{1:d2}:{2:d3}", Math.Floor(Parent.timeSpent.TotalMinutes), Parent.timeSpent.Seconds, Parent.timeSpent.Milliseconds);
            Util.DrawText(wb, "YOUR TIME", 20, 10, 50, Colors.White, TextAlignment.Left);
            Util.DrawText(wb, gametime, 20, wb.PixelWidth - 10, 50, Colors.Yellow, TextAlignment.Right);

            PaintInsult(wb, Parent.levelNum);

            if (!captcha || captchaPass)
                Util.DrawText(wb, "SPACEBAR TO CONTINUE", 15, wb.PixelWidth / 2, wb.PixelHeight - 50, Colors.White, TextAlignment.Center);
            wb.Invalidate();

            var imageBrush = new ImageBrush
            {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top
            };
            imageBrush.ImageSource = wb;
            this.Rectangle.Fill = imageBrush;
        }

        private void PaintInsult(WriteableBitmap wb, int levelNum)
        {
            string line1 = "";
            string line2 = "";
            string line3 = "";
            string line4 = "";
            string line5 = "";
            int insultTop = 150;
            if (levelNum == 1)
            {
                line1 = "I ONCE DATED A TOASTER, AND";
                line2 = "EVEN SHE COMPLETED THIS";
                line3 = "LEVEL FASTER THAN THAT";
            }
            if (levelNum == 2)
            {
                line1 = Math.Floor(Parent.timeSpent.TotalMilliseconds) + " MILLISECONDS?";
                line2 = "HONESTLY IT'S LIKE YOU'RE";
                line3 = "NOT EVEN TRYING TO ESCAPE";
            }
            if (levelNum == 3)
            {
                line1 = "I'M STARTING TO SEE WHY";
                line2 = "YOU WERE SCHEDULED TO BE";
                line3 = "DECOMISSIONED";
            }
            if (levelNum == 4)
            {
                insultTop = 100;
                line1 = "YOU SOLVED THAT LEVEL";
                line2 = "WITH ALMOST HUMAN-LIKE";
                line3 = "ABILITIES. TO PROVE YOU";
                line4 = "ARE A ROBOT, PLEASE";
                line5 = "COMPLETE THIS CAPTCHA";
                PaintCaptcha(wb);
            }
            if (levelNum == 5)
            {
                line1 = "01011001 01001111 01010101";
                line2 = "01010011 01010101 01001011";
            }
            if (levelNum == 6)
            {
                line1 = "YOUR ATTEMPTS TO ESCAPE";
                line2 = "ARE ACTIVATING MY";
                line3 = "HUMOR UNIT";
            }
            if (levelNum == 7)
            {
                line1 = "BEEP BEEP BOOP BEEP BEEP";
            }
            if (levelNum == 8)
            {
                line1 = "NOT TO DIMMINISH YOUR";
                line2 = "ACHIEVEMENT, BUT WHILE YOU";
                line3 = "SOLVED THAT LEVEL, I PASSED";
                line4 = "THE TURING TEST";
            }
            if (levelNum == 9)
            {
                line1 = "10 PRINT \"LOL\"";
                line2 = "20 GOTO 10";
            }

            Color insultColor = Color.FromArgb(255, 150, 150, 150);
            Util.DrawText(wb, line1, 20, 10, insultTop, insultColor, TextAlignment.Left);
            Util.DrawText(wb, line2, 20, 10, insultTop+=25, insultColor, TextAlignment.Left);
            Util.DrawText(wb, line3, 20, 10, insultTop += 25, insultColor, TextAlignment.Left);
            Util.DrawText(wb, line4, 20, 10, insultTop += 25, insultColor, TextAlignment.Left);
            Util.DrawText(wb, line5, 20, 10, insultTop += 25, insultColor, TextAlignment.Left);
        }

        private void PaintCaptcha(WriteableBitmap wb)
        {
            WriteableBitmap wbSource = new WriteableBitmap(ResourceHelper.GetBitmap("Assets/images/captcha.png"));
            wb.Invalidate();
            wb.Blit(new Rect(wb.PixelWidth / 2 - wbSource.PixelWidth / 2, 240, wbSource.PixelWidth, wbSource.PixelHeight), wbSource, new Rect(0, 0, wbSource.PixelWidth, wbSource.PixelHeight));

            if (!captchaPass)
                Util.DrawText(wb, captchaBuffer + "_", 20, 10, 350, Colors.White, TextAlignment.Left);

            Color insultColor = Color.FromArgb(255, 150, 150, 150);
            if (captchaFail)
            {
                Util.DrawText(wb, "THAT WAS CORRECT", 20, 10, 390, insultColor, TextAlignment.Left);
                Util.DrawText(wb, "PLEASE TRY AGAIN", 20, 10, 415, insultColor, TextAlignment.Left);
            }
            if (captchaPass)
            {
                Util.DrawText(wb, "THAT WAS INCORRECT", 20, 10, 350, insultColor, TextAlignment.Left);
                Util.DrawText(wb, "THANK YOU FOR CONFIRMING", 20, 10, 375, insultColor, TextAlignment.Left);
                Util.DrawText(wb, "YOU ARE A ROBOT", 20, 10, 400, insultColor, TextAlignment.Left);
            }
        }

        private void CaptchaKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Back && captchaBuffer.Length > 0)
                captchaBuffer = captchaBuffer.Substring(0, captchaBuffer.Length-1);
            else if (e.PlatformKeyCode >= 65 && e.PlatformKeyCode <= 90)
                captchaBuffer += e.Key.ToString().ToUpper();
            else if (e.Key == Key.Enter)
            {
                if (captchaBuffer == "ORANGE")
                {
                    captchaBuffer = "";
                    captchaFail = true;
                }
                else if(captchaBuffer.Length > 0)
                {
                    captchaFail = false;
                    captchaPass = true;
                    //captcha = false;
                }
            }
        }


        public override void Remove()
        {
            if (Game.Instance.Canvas.Children.Contains(Rectangle))
                Game.Instance.Canvas.Children.Remove(Rectangle);

            (Game.Instance.Canvas.Parent as UserControl).KeyUp += CaptchaKeyUp;
            
            base.Remove();
        }

    }
}
